今天我們會把mySpringbootmall的空殼專案建立起來,本系列是主題是AI&Data,所以關於Spring Boot的知識不會詳加說明,這部分請讀者自行上網學習相關基本知識。
首先,會建立一個mySpringbootmall的專案,自行建立測試用的假資料,用它來測試mySpringbootmall是否可正常執行。
工作目錄 ~/mywork/
專案架構如圖:
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.3</version>
<relativePath/>
</parent>
<modelVersion>4.0.0</modelVersion>
<groupId>com.grace</groupId>
<artifactId>mySpringbootmall</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mySpringbootmall</name>
<description>ML/DL 實作-營養抗老專案</description>
<properties>
<java.version>21</java.version>
<spring.boot.version>3.3.3</spring.boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.grace.mySpringbootmall.controller;
import java.util.Arrays;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.grace.mySpringbootmall.dto.EffectDto;
@RestController
@RequestMapping("/api")
public class EffectController {
@GetMapping("/effects")
public List<EffectDto> listEffects() {
return Arrays.asList(
new EffectDto("E001", "烏髮(減少白髮/促黑色素)", "皮膚毛髮"),
new EffectDto("E002", "抗皺(細紋/彈性)", "皮膚抗老")
);
}
}
package com.grace.mySpringbootmall.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.grace.mySpringbootmall.dto.RecommendationResponse;
import com.grace.mySpringbootmall.service.RecommendationService;
import jakarta.validation.constraints.Pattern;
@RestController
@RequestMapping("/api")
@Validated
public class RecommendationController {
@Autowired
private RecommendationService recommendationService;
@GetMapping("/recommendations")
public RecommendationResponse recommend(
@RequestParam
String effectId) {
return recommendationService.recommend(effectId);
}
}
package com.grace.mySpringbootmall.dto;
public class EffectDto {
private String effectId; // E001 / E002
private String effectName; // 烏髮 / 抗皺
private String category; // 皮膚毛髮 / 皮膚抗老
public EffectDto() {}
public EffectDto(String effectId, String effectName, String category) {
this.effectId = effectId;
this.effectName = effectName;
this.category = category;
}
public String getEffectId() { return effectId; }
public void setEffectId(String effectId) { this.effectId = effectId; }
public String getEffectName() { return effectName; }
public void setEffectName(String effectName) { this.effectName = effectName; }
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
}
package com.grace.mySpringbootmall.service.scoring;
import java.util.Arrays;
import java.util.List;
import org.springframework.stereotype.Component;
import com.grace.mySpringbootmall.dto.RecommendationItem;
@Component
public class MockScoringStrategy implements ScoringStrategy {
@Override
public List<RecommendationItem> score(String effectId) {
if ("E001".equalsIgnoreCase(effectId)) { // 烏髮
return Arrays.asList(
new RecommendationItem("food", "黑芝麻", 7.2, "C", Arrays.asList("PMID:demo")),
new RecommendationItem("food", "綠茶", 6.5, "C", Arrays.asList("PMID:demo")),
new RecommendationItem("food", "雞蛋(維生素B12來源)", 6.0, "C", Arrays.asList("PMID:demo"))
);
} else if ("E002".equalsIgnoreCase(effectId)) { // 抗皺
return Arrays.asList(
new RecommendationItem("food", "番茄", 8.0, "B", Arrays.asList("PMID:demo")),
new RecommendationItem("food", "甜椒(維生素C)", 7.5, "C", Arrays.asList("PMID:demo")),
new RecommendationItem("food", "鮭魚", 7.0, "C", Arrays.asList("PMID:demo"))
);
}
return Arrays.asList();
}
}
package com.grace.mySpringbootmall.service.scoring;
import java.util.List;
import com.grace.mySpringbootmall.dto.RecommendationItem;
public interface ScoringStrategy {
List<RecommendationItem> score(String effectId);
}
package com.grace.mySpringbootmall.service;
import com.grace.mySpringbootmall.dto.RecommendationResponse;
public interface RecommendationService {
RecommendationResponse recommend(String effectId);
}
package com.grace.mySpringbootmall.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.grace.mySpringbootmall.dto.RecommendationItem;
import com.grace.mySpringbootmall.dto.RecommendationResponse;
import com.grace.mySpringbootmall.service.scoring.ScoringStrategy;
@Service
public class RecommendationServiceImpl implements RecommendationService {
@Autowired
private ScoringStrategy scoring;
@Override
public RecommendationResponse recommend(String effectId) {
List<RecommendationItem> items = scoring.score(effectId);
return new RecommendationResponse(effectId, items);
}
}
server.port=8080
spring.main.banner-mode=off
logging.level.org.springframework.web=INFO
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
測試方法,在terminal(~/mywork/mySpringbootmall):
mvn clean package -skipTests
java -jar target/mySpringbootmall-0.0.1-SNAPSHOT.jar